package UDP;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class UDP2 extends JFrame  {

	/**
	 * 2019 04 11 刘相超
	 */
	private static final long serialVersionUID = 1L;
	private static String IP;		//IP 地址,为实现简单，写定127.0.0.1不可修改
	public UDP2() {
		// TODO Auto-generated constructor stub
		UDP2.IP="127.0.0.1";
	}
	
	//初始化函数
	public void Initial(int port_s,int port_r) {
		JFrame jf= new JFrame("UDP程序，本程序端口"+ port_s+"目标端口"+port_r);
		jf.setSize(400,500);
		jf.setLayout(null);
		
		JLabel jl_1=new JLabel("请输入要发送的内容：");
		jl_1.setLocation(50,0);
		jl_1.setSize(400, 100);
		jl_1.setVisible(true);
		
		JTextField jtf_1= new JTextField();
		jtf_1.setEnabled(true);
		jtf_1.setBounds(50, 60, 300, 50);
		
		JTextField jtf_2= new JTextField();
		jtf_2.setEnabled(false);
		jtf_2.setBounds(50, 300, 300, 50);
		

		JButton jbt_1 = new JButton("发送");
		jbt_1.setSize(200, 50);
		jbt_1.setLocation(100,150);
		jbt_1.addActionListener(new ActionListener()//发送
				{

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						jbt_1.setEnabled(false);
						jbt_1.setEnabled(true);
						String str= jtf_1.getText();
						
						//建立UDP socket端点 
				        DatagramSocket s = null;
						try {
							s = new DatagramSocket();
						} catch (SocketException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
				        //提供数据，封装打包       
				        byte[] bs=str.getBytes();
				        DatagramPacket dp = null;
						try {
							dp = new DatagramPacket(bs, bs.length, InetAddress.getByName(IP), port_r);
						} catch (UnknownHostException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				        //使用send发送
				        try {
				            s.send(dp);
				        } 
				        catch (IOException ex) {
				            System.out.println("发送失败： ");
				            ex.printStackTrace();
				        }
				        
				        //关闭资源 
				        s.close();
					}
			
				});
		
		JButton jbt_2 = new JButton("接收");
		jbt_2.setSize(200, 50);
		jbt_2.setLocation(100,220);
		jbt_2.addActionListener(new ActionListener()//接收
				{

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						 //建立UDP socket，设置接收端口
						jbt_2.setEnabled(false);
						jbt_2.setEnabled(true);
				        DatagramSocket ds = null;
						try {
							ds = new DatagramSocket(port_s);
						} catch (SocketException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

				        //预先创建数据存放的位置，封装
				        byte [] bbuf = new byte [1024];
				         DatagramPacket dp = new DatagramPacket(bbuf,bbuf.length);
				         
				         //使用receive阻塞式接收
				         try {
							ds.receive(dp);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				         	         
				         /**4、关闭资源*/
				         ds.close();
				         jtf_2.setText(new String(dp.getData()));
					}
			
				});
		
		JLabel jl_2=new JLabel("收到的内容：");
		jl_2.setLocation(50, 240);
		jl_2.setSize(400, 100);
		jl_2.setVisible(true);
		
		
		jf.add(jbt_1);
		jf.add(jtf_1);
		jf.add(jl_1);
		jf.add(jtf_2);
		jf.add(jl_2);
		jf.add(jbt_2);
		
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.addWindowListener(new WindowAdapter(){
			@SuppressWarnings("unused")
			public void JFClose(WindowEvent w){
				System.exit(0);
			}
		});
		jf.setVisible(true);
	}
	
	/**步骤：---
	 * 1、建立UDP socket 接收和发送点
	 * 2、提供数据，打包
	 * 3、使用send发送
	 * 4、关闭资源
	 * */
	
	//主函数
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UDP2 udp2=new UDP2();
		int a,b;
		if(args.length<2)
			System.out.println("请输入本程序端口号以及目标程序端口号：");
		else {
			a= Integer.parseInt(args[0]);
			b= Integer.parseInt(args[1]);
			udp2.Initial(a,b);
			}
	}

}
